package com.byb.server.service.util;

import com.byb.server.domain.Product;
import com.byb.server.service.dto.ProductDetail;
import com.byb.server.service.dto.Product_sort;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Util  for sort of  Product
 */
public class SortUtil {

    /**
     *  products 不为空与 list 比较，将相同元素取出
     * @param list 费率数据
     * @param products 关键词检索产品数据
     * @param productId 费率产品数据
     * @param payment 年缴费
     * @return the list of entities
     */
   public static List<Product_sort> filterProduct(List<Product_sort> list,List<Product> products, List<Product> productId,Integer payment){
       List<Product_sort> data = new  ArrayList<>();
       List<Product> fs=new ArrayList<>();
       if(products!=null){
           for (int i = 0; i < products.size(); i++) {
               for (int j = 0; j < list.size(); j++) {
                   if (list.get(j).getProductId() == products.get(i).getId()) {
                       data.add(list.get(j));
                       fs.add(productId.get(j));
                   }
               }
           }
           return countMoney(data,fs,payment);
       }else{
           return countMoney(list,productId,payment);
       }
   }

    /**
     * Calculate item of list according to payment
     * 默认费率表保费为10000
     *
     * @param data
     * @param payment
     * @return the list of entities
     */
    private static List<Product_sort> countMoney(List<Product_sort> data,List<Product> productId,Integer payment){
        List<Product_sort> mids=new ArrayList<>();
        mids.addAll(data);
        for(int i=0; i<mids.size();i++){
            if(mids.get(i).getPrice()==0.0f){
                mids.remove(i);
            }
        }
        List<Product_sort> list= mids.stream().sorted(Comparator.comparing(Product_sort::getPrice)).collect(Collectors.toList());
        DecimalFormat df = new DecimalFormat("#0.0");
        for(int i =0; i<list.size(); i++){
            list.get(i).setStrichen_money(String.valueOf(df.format(payment/list.get(i).getPrice())));
            list.get(i).setMild_money(String.valueOf(df.format(payment/list.get(i).getPrice()*Double.parseDouble(productId.get(i).getMildProbability()))));
            list.get(i).setDie_money(String.valueOf(df.format(payment/list.get(i).getPrice()*Double.parseDouble(productId.get(i).getDcRate()))));
            list.get(i).setAmountOfIndemnificationSocre(String.valueOf(df.format((payment/list.get(i).getPrice())/(payment/list.get(0).getPrice())*10)));
            list.get(i).setOverallScore(String.valueOf(df.format(Double.parseDouble(list.get(i).getEnsureScopeSocre())*Double.parseDouble(list.get(i).getClaimsSettlementSocre())*Double.parseDouble(list.get(i).getAmountOfIndemnificationSocre())*Double.parseDouble(list.get(i).getConvenientServicetSocre())/100)));
        }
        return list;
    }

    public static ProductDetail moneyUtil(ProductDetail productDetail,Integer payment, List<Product_sort> list){
//        DecimalFormat df = new DecimalFormat("#0.0");
//        productDetail.setStrichen_money(String.valueOf(df.format(payment  / Double.parseDouble(productDetail.getPrice()))));
//        productDetail.setMild_money(String.valueOf(df.format((payment ) / Double.parseDouble(productDetail.getPrice())*Double.parseDouble(productDetail.getMildProbability()))));
//        productDetail.setDie_money(String.valueOf(df.format((payment ) / Double.parseDouble(productDetail.getPrice()))));
//        productDetail.setAmountOfIndemnificationSocre(String.valueOf(df.format((payment/Double.parseDouble(productDetail.getPrice()))/(payment/list.get(0).getPrice())*10)));
//        productDetail.setOverallScore(String.valueOf(df.format(Double.parseDouble(productDetail.getEnsureScopeSocre())*Double.parseDouble(productDetail.getClaimsSettlementSocre())*Double.parseDouble(productDetail.getAmountOfIndemnificationSocre())*Double.parseDouble(productDetail.getConvenientServicetSocre())/100)));
        return productDetail;
    }
}
